﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace KTSonline
{
    public class ShoppingCart
    {
        public System.Collections.Generic.List<ProductCart> lstCart { get; set; }
        public Int32 quantity { get; set; }
        public Int32 totalprice { get; set; }

        public ShoppingCart()
        {
            lstCart = new System.Collections.Generic.List<ProductCart>();
            quantity = 0;
            totalprice = 0;
        }

        public void AddProduct(ProductCart pd)
        {
            Boolean f = false;
            for (Int32 i = 0; i < lstCart.Count; i++ )
            {
                if (lstCart[i].id == pd.id)
                {
                    f = true;
                    lstCart[i].quantity += pd.quantity;
                }
            }
            if(!f)
                lstCart.Add(pd);

            quantity += pd.quantity;
            totalprice += pd.quantity * pd.price;
        }
    }

    public class ProductCart
    {
        public String id { get; set; }
        public String name { get; set; }
        public String thumbnail { get; set; }
        public String category { get; set; }
        public Int32 price { get; set; }
        public Int32 quantity { get; set; }

        public ProductCart()
        {
        }
        public ProductCart(String id)
        {
            this.id = id;
            this.quantity = 1;
            getInfo();
        }

        private void getInfo()
        {
            KTSonlineDataContext db = new KTSonlineDataContext();
            var rs = db.spa_LayThongTinSanPhamDatHang(this.id).First();
            this.name = rs.tenmay;
            this.price = (Int32)rs.giaban;
            this.thumbnail = rs.thumbnail;
            this.category = rs.loaihang;
        }
    }
}
